
package pl.genapp.stocksense.libraries;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import pl.genapp.stocksense.others.PostExecuteReaction;
import android.content.Context;
import android.os.AsyncTask;

public class HistoricalDataProvider extends AsyncTask<String, Void, List<HistoricalDataProvider.HistoricalDataField>> {
	
	/**
	 * Klasa przechowująca dane z wiersza
	 * 
	 * @author dominik
	 * 
	 */
	
	public static class HistoricalDataField {
		public Date		date;
		public Double	open;
		public Double	high;
		public Double	low;
		public Double	close;
		public Double	volume;
		public Double	adjClose;
	}
	
	Context																	context;
	PostExecuteReaction<List<HistoricalDataProvider.HistoricalDataField>>	reaction;
	
	public HistoricalDataProvider(Context context, PostExecuteReaction<List<HistoricalDataProvider.HistoricalDataField>> reaction) {
		this.context = context;
		this.reaction = reaction;
	}
	
	protected List<HistoricalDataProvider.HistoricalDataField> doInBackground(String... params) {
		
		int rowNumber = 0;
		List<HistoricalDataProvider.HistoricalDataField> result = new ArrayList<HistoricalDataProvider.HistoricalDataField>();
		
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(params[0]);
			
			HttpResponse response = client.execute(request);
			
			BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			
			/**
			 * Odczytujemy dane line by line
			 */
			String line;
			
			while ((line = rd.readLine()) != null) {
				
				String[] RowData = line.split(",");
				
				if (rowNumber > 0) {
					
					HistoricalDataField fields = new HistoricalDataField();
					result.add(fields);
					
					for (int k = 0; k < RowData.length; k++) {
						
						switch (k) {
						
							case 0:
								SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
								try {
									fields.date = (Date) df.parse(RowData[0]);
								} catch (ParseException e) {
									e.printStackTrace();
								}  
								break;
							case 1:
								fields.open = Double.parseDouble(RowData[1]);
								break;
							case 2:
								fields.high = Double.parseDouble(RowData[2]);
								break;
							case 3:
								fields.low = Double.parseDouble(RowData[3]);
								break;
							case 4:
								fields.close = Double.parseDouble(RowData[4]);
								break;
							case 5:
								fields.volume = Double.parseDouble(RowData[5]);
								break;
							case 6:
								fields.adjClose = Double.parseDouble(RowData[6]);
								break;
						
						}
						
					}
					
				}
				
				rowNumber++;
			}
			
			// odracamy kolejnosc; 
			Collections.reverse(result);

			
			return result;
			
		} catch (IOException e) {
			
		}
		
		return null;
		
	}
	
	protected void onPostExecute(List<HistoricalDataProvider.HistoricalDataField> result) {
		
		if (result != null) {
			if (reaction != null) reaction.onSuccess(context, result);
		} else
			reaction.onError(context, result);
		
	}
	
}
